<?php
namespace Admin\Model;
use Think\Model;

class MeetingModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE),
        array('MeetingTime','require','请选择会议时间',Model::MUST_VALIDATE),
        array('MeetingType','number','请选择会议类型',Model::MUST_VALIDATE)
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		$where = " 1 ";
		foreach($parmArr AS $key => $value){
			$where .= " AND $key = '$value'"; 
		}
		$sql = "SELECT a.* FROM meeting AS a WHERE $where";
		$res = $this->query($sql);
		return $res;
	}
	
	public function MyshowList(){
		$where = " 1 ";
		$keyword = I('keyword');
		/*
		if(!empty($keyword)){
			$sql = "SELECT ID FROM project WHERE Name = '{$keyword}'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND ProjectID = ".$res[0]['ID'];
			}else{
				$where .= " AND ProjectID = -1";
			}
		}
		*/
		if(!empty($keyword)){
			$where .= " AND HandlePerson LIKE '%{$keyword}%'";
		}
		$id = (int)I('id');
		if(!empty($id)){
			$where .= " AND ProjectID = ".$id;
		}
		$return = array();
		$sql = " SELECT COUNT(*) count FROM meeting WHERE $where ";
		$count = $this->query($sql);
		$count = $count[0]['count'];
	    $page = $this->Mypage($count, 8);
		$sql = "SELECT a.ID,a.MeetingTime,a.HandlePerson,(SELECT Name FROM project WHERE ID = a.ProjectID) AS ProjectName FROM meeting AS a WHERE $where ORDER BY ID DESC LIMIT  $page->firstRow , $page->listRows";
		$res = $this->query($sql);
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
}

?>				